package cn.wolfcode.luowowo.service.impl;

import cn.wolfcode.luowowo.domain.Strategy;
import cn.wolfcode.luowowo.domain.StrategyCatalog;
import cn.wolfcode.luowowo.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.repository.StrategyCatalogRepository;
import cn.wolfcode.luowowo.service.IStrategyCatalogService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.util.DBHelper;
import cn.wolfcode.luowowo.vo.CatalogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
//@Transactional mongodb事务必须复制集方式
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {
    

    //Spring-boot集成redis持久化数据库
    @Autowired
    private StrategyCatalogRepository repository;
    
    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyService strategyService;

    @Override
    public void save(StrategyCatalog strategyCatalog) {

        strategyCatalog.setId(null);
        repository.save(strategyCatalog);
    }

    @Override
    public void delete(String id) {

        repository.deleteById(id);
    }

    @Override
    public void update(StrategyCatalog strategyCatalog) {

        repository.save(strategyCatalog);
    }

    @Override
    public StrategyCatalog get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyCatalog> listAll() {
        return repository.findAll();
    }


    // 攻略分类列表更新/添加
    @Override
    public void saveOrUpdate(StrategyCatalog strategyCatalog) {

        if(StringUtils.hasLength(strategyCatalog.getId())){
           this.update(strategyCatalog);
        }else{
            this.save(strategyCatalog);
        }
    }


    // 攻略分类分页查询
    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {

        Query query = new Query();

        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1,
                qo.getPageSize(), Sort.Direction.DESC,"_id");
        
        return DBHelper.query(template,StrategyCatalog.class,query,pageable);
    }

    // 攻略分组下拉框
    @Override
    public List<CatalogVO> groupList() {

        // 整个数据看做一个集合
        List<CatalogVO> list = new ArrayList<>();

        // 开始组装
        // Mongodb的聚合查询(分组查询)
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);

        //看做map = key  value,  一行key 一行值
        List<Map> datas = result.getMappedResults();

        for (Map data : datas) {//每行都是集合
            // 每一行都是vo对象
            CatalogVO vo = new CatalogVO();
            // 获取_id字段,因为_id是字符串类型所以toString
            vo.setDestName(data.get("_id").toString());
            // 从一行的集合中获取names()属性(攻略分类的名称集合)
            List<Object> names = (List<Object>) data.get("names");
            // 攻略分类ids集合
            List<Object> ids = (List<Object>) data.get("ids");

            // 要把对应的集合添加到(攻略分类对象的集合)
            List<StrategyCatalog> catalogList = new ArrayList<>();

            // 把所有的目的地名字遍历出来
            for (int i=0; i<names.size() ;i++) {
                // 创建攻略分类对象
                StrategyCatalog sc = new StrategyCatalog();
                // 从目的地集合中获取单一目的地
                String name = names.get(i).toString();
                // 从目的地ids集合中获取单一id
                String id = ids.get(i).toString();
                // 把其中一个目的地名字设置到攻略分类列表中
                sc.setName(name);
                sc.setId(id);
                // 把每个攻略分类对象设置到集合中
                catalogList.add(sc);

            }
            // 把攻略分类对象集合添加到vo对象中
            vo.setCatalogList(catalogList);

            // 把vo的每一行数据放到这个大集合中
            list.add(vo);
        }
        System.out.println(list);
        return list;
    }

    // 查询分类所对应的攻略
    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String destId) {

        // 1.查询指定destId的所有分类
        List<StrategyCatalog> list = repository.findByDestId(destId);

        for (StrategyCatalog strategyCatalog : list) {

            List<Strategy> sts= strategyService.queryCatalogId(strategyCatalog.getId());
            strategyCatalog.setStrategies(sts);
        }
        return list;
    }


}
